package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.browserVersion;

import com.alibaba.fastjson.JSONArray;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.BrowserVersionMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.BrowserVersion;
import com.cqhilink.iot.jobstandardmanagement.facade.service.browserVersion.JsmBrowserVersionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 用户登录记录浏览器信息实现类
 * @CreateDateTime 2017-07-19 9:39
 **/
public class JsmBrowserVersionServiceImpl implements JsmBrowserVersionService {
    @Autowired
    private BrowserVersionMapper browserVersionMapper;

    /**
     * 新增
     *
     * @param userId
     * @param bv
     * @return
     */
    @Override
    public Map<String, Object> save(String userId, String bv) {
        if (bv == null) {
            return null;
        }
        BrowserVersion param = JSONArray.parseObject(bv, BrowserVersion.class);
        if (param == null) {
            return null;
        }
        param.setUserId(userId);
        param.setRecordId(UniqueUtil.uuid());
        param.setLoginTime(new Date());
        param.setBrowserName(param.getName());
        int num = browserVersionMapper.insert(param);
        Map<String, Object> map = new HashMap<String, Object>();
        if (num == 0) {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
            map.put("msg", BackMessage.ADD_FAIL);
        } else {
            map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            map.put("msg", BackMessage.ADD_OK);
        }
        return map;
    }
}
